import os


class Test():

    def read_log(self, file_path):
        with open(file_path, 'r', encoding="utf-8") as f:
            for line in f.read().splitlines():
                storeId = orderNo = posNo = payType = chnlType = tranType = RespCode = tranDate = ""
                if "|0|0||0||Trade" in line:
                    _str = line.split(":")
                    _l = _str[1].split(" ")
                    pay_info =[x for x in _l if x != "" and len(x)<90]
                    if len(pay_info[0]) == 8:
                        storeId = pay_info[0][:4]
                        orderNo = max(pay_info, key=len)
                        posNo = pay_info[0]
                        payType = pay_info[2][-3:]
                        chnlType = "POS"
                        tranType = pay_info[2][0]
                        print(f"{storeId}, {orderNo}, {posNo}, {payType}, {chnlType}, {tranType},")
                        with open("./pos.txt", 'a', encoding="utf-8") as f1:
                            f1.write(f"{storeId}, {orderNo}, {posNo}, {payType}, {chnlType}, {tranType},")
                            f1.close()
                    else:
                        RespCode = pay_info[0]
                        if len(RespCode) > 2:
                            RespCode = RespCode[0:2]
                        time = _str[0].split('|')[2].split('.')[0]
                        data = _str[0].split('|')[1]
                        tranDate = f"{data}{time}"
                        print(f"{RespCode}, {tranDate}")
                        with open("./pos.txt", 'a', encoding="utf-8") as f2:
                            f2.write(f"{RespCode}, {tranDate} \n")
                            f2.close()

    def run(self):
        base_path = r"D:\Disktop\新建文件夹"
        # base_path = r"D:\Disktop\新建文件夹 (9)"
        for root, dir, fils in os.walk(base_path):
            for fil in fils:
                abs_path = os.path.join(root, fil)
                self.read_log(abs_path)


if __name__ == '__main__':
    # file = r'D:\Disktop\新文件 2.txt'
    # file = r'D:\Disktop\新建文件夹\1009\pos\SHGS9T1-1-TPDotnet.Pos-20240125153450.6923.log'
    t = Test()
    t.run()
